<div class="layui-fluid" lay-title="通知公告表单">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="1" title="通知" checked="">
                        <input type="radio" name="type" value="2" title="公告">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="有效" checked="">
                        <input type="radio" name="status" value="0" title="无效">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block" id="editor">

                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="form-group-submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    layui.use(['jquery', 'admin', 'form', 'layer','conf','wangEditor'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , conf = layui.conf
            , admin = layui.admin;
        var sysNotice = {};
        form.render(null, 'form-group');
        var id = POPUP_DATA.id;
        var table = POPUP_DATA.table;
        // 编辑器
        var E =  layui.wangEditor;
        var editor = new E("#editor");
        editor.customConfig.uploadImgParams = {
            token: admin.loginToken
        };
        editor.customConfig.uploadFileName = 'myfiles';
        editor.customConfig.uploadImgServer = conf.requestUrl + 'fileUpload';
        editor.create();

        if (id) {
            // 初始化
            admin.get({
                type: 'get',
                url: '/sysNotice/info/' + id,
                dataType: 'json',
                success: function (data) {
                    sysNotice = data.sysNotice;
                    if (sysNotice) {
                        form.val("form-group", {
                            "title": sysNotice.title // "name": "value"
                            , "status": sysNotice.status
                            , "type": sysNotice.type
                        });
                        editor.txt.html(sysNotice.content);
                    }
                }
            });
        }
        /* 监听提交 */
        form.on('submit(form-group-submit)', function (data) {
            var field = data.field;
            var noticeData = {};
            noticeData.status = field.status;
            noticeData.title = field.title;
            noticeData.type = field.type;
            // editor
            var content = editor.txt.html();
            noticeData.content = content;
            var url = "/sysNotice/save";
            if (id) {
                url = "/sysNotice/update?_method=PUT";
                noticeData.id = id;
            }
            admin.post({
                url: url,
                contentType: "application/json",
                data: JSON.stringify(noticeData),
                success: function (res) {
                    if (res.code == 0) {
                        // 提醒保存成功，关闭当前弹窗
                        layer.msg(res.msg);
                        layer.closeAll();
                        //刷新
                        table.reload('list-table-notice', {
                            url: '/sysNotice/page?token=' + admin.loginToken,
                            page: {
                                curr: 1
                            }
                        })
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>